// pages/singlePage.js
var route = require('../../utils/route');
var config = require('../../config/config');
var httpClient = require('../../utils/httpClient');
var common = require('../../utils/common');
var enum_type = require('../../utils/enum');
var util = require('../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },
  loadClickData: function (options) {

  var page = util.getCurrentPageUrl();
  var dataPrams = {
    type: enum_type.pageTypeEnum.SINGLE_PAGE,
    page: page
  };
  var source = options.source;
  if (null == source) {
    dataPrams.jumpType = enum_type.jumpTypeEnum.DEFAULT;
  } else {
    dataPrams.source = source;
    dataPrams.jumpType = enum_type.jumpTypeEnum.COMING;
  }
  common.saveClickData(dataPrams);
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.loadClickData(options);

    var that = this;
    var method = route.getSinglePage();
    var params = {
      appid: config.appId,
    };
    httpClient.request(method, 'GET', params, function (res, err) {
      var title = res.title;
      wx.setNavigationBarTitle({
        title: title//页面标题为路由参数
      });
      that.setData({
        item: res
      });

      var defaultJumpType = res.defaultJumpType;
      if (defaultJumpType==2){
        debugger
        that.jumpClickData(enum_type.pageTypeEnum.SINGLE_PAGE, res.id);
        if (res.jumpType==1){
          var details = res.detail;
          wx.setStorageSync('details', details);
          that.navigateToDetail(); 
        } else if (res.jumpType == 2){
          that.navigateToH5(res.h5Url); 
        } else if (res.jumpType == 3) {
          that.navigateToMiniProgram(res.appId, res.page); 
        }      
      }
     
    });
  },
  jumpClickData: function (type, dataId) {
    var page = util.getCurrentPageUrl();
    var dataPrams = {
      type: type,
      page: page,
      dataId: dataId,
      jumpType: enum_type.jumpTypeEnum.OUT
    };

    common.saveClickData(dataPrams);
  },
  jumpPage: function (event) {

    var item = event.currentTarget.dataset.item;
    var type = event.currentTarget.dataset.type;
    var dataId = item.id;
    this.jumpClickData(type, dataId);

    if (item.jumpType == 1) {
      var details = item.detail;
      wx.setStorageSync('details', details);
      this.navigateToDetail();
    } else if (item.jumpType == 2) {
      this.navigateToMiniProgram(item.appId, item.appPage);
    } else if (item.jumpType == 3) {
      this.navigateToH5(item.h5Url);
    }
  },
  navigateToH5: function (h5Url) {
    wx.navigateTo({
      url: '../webview/webview?h5Url=' + h5Url,
    })
  }, navigateToDetail: function () {
    wx.navigateTo({
      url: '../detail/detail',
    })
  }, navigateToMiniProgram: function (appid, page) {
    wx.navigateToMiniProgram({
      appId: appid,
      path: page,
      envVersion: 'trial',
      success(res) {
        // 打开成功
        console.log(res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})